package cn.zz.cameralibrary.view.holocolorpicker;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.PopupWindow;

import cn.zz.cameralibrary.R;
import cn.zz.cameralibrary.view.holocolorpicker.ColorPicker.OnColorChangedListener;
import cn.zz.cameralibrary.view.holocolorpicker.ColorPicker.OnColorSelectedListener;

/**
 * author: zhu on 2017/7/12 15:06
 * email: mackkill@gmail.com
 */

public class SelectColorPopup extends PopupWindow implements OnColorChangedListener, OnColorSelectedListener {


    private View mMenuView;
    private ColorPicker picker;

    private onClickDismisser onClickDismisser;

    public int getColor() {
        return picker.getColor();
    }

    public SelectColorPopup(Activity context) {
        super(context);
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mMenuView = inflater.inflate(R.layout.camerasdk_select_color, null);
        picker = (ColorPicker) mMenuView.findViewById(R.id.picker);
        SVBar svBar = (SVBar) mMenuView.findViewById(R.id.svbar);
        OpacityBar opacityBar = (OpacityBar) mMenuView.findViewById(R.id.opacitybar);
        SaturationBar saturationBar = (SaturationBar) mMenuView.findViewById(R.id.saturationbar);
        ValueBar valueBar = (ValueBar) mMenuView.findViewById(R.id.valuebar);
        Button submit = (Button) mMenuView.findViewById(R.id.submit);
        submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onClickDismisser != null)
                    onClickDismisser.onDissmiss(getColor());
                dismiss();
            }
        });
        picker.addSVBar(svBar);
        picker.addOpacityBar(opacityBar);
        picker.addSaturationBar(saturationBar);
        picker.addValueBar(valueBar);

        //To get the color
        picker.getColor();

        //To set the old selected color u can do it like this
        picker.setOldCenterColor(picker.getColor());
        // adds listener to the colorpicker which is implemented
        //in the activity
        picker.setOnColorChangedListener(this);
        picker.setOnColorSelectedListener(this);
        this.setContentView(mMenuView);
        this.setWidth(ViewGroup.LayoutParams.FILL_PARENT);
        this.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        this.setFocusable(true);
        ColorDrawable dw = new ColorDrawable(0xb0000000);
        this.setBackgroundDrawable(dw);
        mMenuView.setOnTouchListener(new View.OnTouchListener() {
            public boolean onTouch(View v, MotionEvent event) {
                int height = mMenuView.findViewById(R.id.pop_layout).getTop();
                int y = (int) event.getY();
                if (event.getAction() == MotionEvent.ACTION_UP) {
                    if (y < height) {
                        if (onClickDismisser != null)
                            onClickDismisser.onDissmiss(getColor());
                        dismiss();
                    }
                }
                return true;
            }
        });

    }

    @Override
    public void onColorChanged(int color) {
    }

    @Override
    public void onColorSelected(int color) {
    }

    public interface onClickDismisser {
       void onDissmiss(int color);
    }

    public void setOnClickDismisser (onClickDismisser onClickDismisser) {
        this.onClickDismisser =onClickDismisser;
    }
}